package com.project.weimi.activity;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.project.weimi.R;
import com.project.weimi.application.Constant;
import com.project.weimi.entity.response.LoginSuccessBean;
import com.project.weimi.interfaces.MyCallBack;
import com.project.weimi.utils.CheckUtils;
import com.project.weimi.utils.GsonUtil;
import com.project.weimi.utils.NetUtils;
import com.project.weimi.utils.SetClickListener;
import com.project.weimi.utils.SharedPreferencesHelper;
import com.project.weimi.utils.XUtil;
import com.project.weimi.widget.ToastMessage;
import com.project.weimi.widget.UnderLineEditText;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class LoginActivity extends BaseActivity {

	private Context mContext;
	private UnderLineEditText et_phone, et_secret;
	private TextView tv_tips, tv_freeRegister, tv_forgetPwd;
	private Button btn_login;
	private ImageView img_weibo, img_weixin, img_qq;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentViewWithBar(R.layout.activity_login, "登录");

		initView();

	}

	private void initView() {
		mContext = this;
		et_phone = (UnderLineEditText) findViewById(R.id.et_phone);
		et_secret = (UnderLineEditText) findViewById(R.id.et_secret);
		tv_tips = (TextView) findViewById(R.id.tv_tips);
		tv_freeRegister = (TextView) findViewById(R.id.tv_freeRegister);
		tv_forgetPwd = (TextView) findViewById(R.id.tv_forgetPwd);
		btn_login = (Button) findViewById(R.id.btn_login);
		img_weibo = (ImageView) findViewById(R.id.img_weibo);
		img_weixin = (ImageView) findViewById(R.id.img_weixin);
		img_qq = (ImageView) findViewById(R.id.img_qq);

		// 监听
		SetClickListener.setClickListener(listener, tv_freeRegister, tv_forgetPwd, btn_login, 
				img_weibo, img_weixin,img_qq);
	}

	private OnClickListener listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent = null;
			switch (v.getId()) {
			case R.id.tv_freeRegister: // 跳到“注册”页
				intent = new Intent(mContext, RegisterActivity.class);
				startActivity(intent);
				break;
			case R.id.tv_forgetPwd: // 跳到“忘记密码”页
				ToastMessage.showMyToast(mContext, "跳到忘记密码页面");
				break;
			case R.id.btn_login: // 登录
				
				if(!NetUtils.isConnected(LoginActivity.this)){
					ToastMessage.showToast(LoginActivity.this, "网络不给力，请检查网络设置");
				} else {
					initLogin();
				}

				break;
			case R.id.img_weibo:
				ToastMessage.showMyToast(mContext, "跳到微博页面");
				break;
			case R.id.img_weixin:
				ToastMessage.showMyToast(mContext, "跳到微信页面");
				break;
			case R.id.img_qq:
				ToastMessage.showMyToast(mContext, "QQ页面");
				break;
			default:
				break;
			}
		}
	};

	private void initLogin() {
		String phone = et_phone.getText().toString().replace(" ", "").trim();
		String secret = et_secret.getText().toString().replace(" ", "").trim();
		if (TextUtils.isEmpty(phone)) {
			//tv_tips.setText("");
			ToastMessage.showMyToast(LoginActivity.this, "请输入手机号码！");
			return;
		}
		if (!phone.matches(CheckUtils.phoneNum)) {
			//tv_tips.setText("请输入正确手机号码！");
			ToastMessage.showMyToast(LoginActivity.this, "请输入正确手机号码！");
			return;
		}
		if (TextUtils.isEmpty(secret)) {
			//tv_tips.setText("请输入密码！");
			ToastMessage.showMyToast(LoginActivity.this, "请输入密码！");
			return;
		}
		if (secret.length() < 6) {
			//tv_tips.setText("密码 长度在6-18位之间");
			ToastMessage.showMyToast(LoginActivity.this, "密码 长度在6-18位之间");
			return;
		}
		if (!secret.matches(CheckUtils.pwdPattern)) {
			//tv_tips.setText("必须字母开头，可以为纯字母，也可以为数字、字母组合");
			ToastMessage.showMyToast(LoginActivity.this, "必须字母开头，可以为纯字母，也可以为数字、字母组合");
			return;
		}

		// 发送登录请求
		sendLoginRequestHttp(phone, secret);

	}

	private void sendLoginRequestHttp(String memberId, String passWord) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("memberId", memberId);
		map.put("passWord", passWord);

		XUtil.post(Constant.LOGIN, map, new MyCallBack<String>() {

			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				if (null != result && !"".equals(result)) {

					//Log.e("TAG", "登录返回===" + result);
					try {
						JSONObject obj = new JSONObject(result);
						if (!TextUtils.isEmpty(obj.getString("result"))
								&& TextUtils.equals(obj.getString("result"), "error")) {
							
							ToastMessage.showMyToast(LoginActivity.this, "登录失败");
							//Log.e("TAG", "登录返回===" + "登录失败");

						} else if (obj.getJSONObject("result").has("memberId")) {
							LoginSuccessBean bean = (LoginSuccessBean) GsonUtil.jsonToBean(
									obj.getString("result"), LoginSuccessBean.class);
						    Log.e("TAG", "登录成功++++" + bean.toString());
							ToastMessage.showMyToast(LoginActivity.this, "登录成功！");

							// 保存用户信息
							SharedPreferencesHelper spHelper = new SharedPreferencesHelper(
									LoginActivity.this,Constant.USER_INFO);
							spHelper.clear();
							spHelper.put("memberId", bean.getMemberId());
							spHelper.put("nickName", bean.getMemberName());
							spHelper.put("sex", bean.getSex());
							spHelper.put("age", bean.getAge());
							spHelper.put("sign", bean.getSign());
							spHelper.put("address", bean.getAddress());
							spHelper.put("headImgPath", bean.getHeadImgPath());
							spHelper.put("integrate", bean.getIntegrate());
							
							LoginActivity.this.finish();

						}
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
			}

			@Override
			public void onError(Throwable ex, boolean isOnCallback) {
				super.onError(ex, isOnCallback);
			}
		});
	}

	@Override
	void setStatusBar(int statusBarColorRes) {
		super.setStatusBar(R.color.transparent_3);
	}

}
